﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Default3 : System.Web.UI.Page
{
    string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["xxnewsConnectionString"].ConnectionString;
    SqlConnection sqlcon;
    SqlCommand sqlcom;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["pn"] == "" || Session["pn"] == null)
        {
            Response.Redirect("default.aspx");
        }
        if (Session["pngeren"] == "" || Session["pngeren"] == null)
        {
            Response.Redirect("default.aspx");
        }
        if (Session["pn"].ToString().Contains("设备科") == false)
        {
            Response.Redirect("default.aspx?lb=基础数据");
        }
        //if (Session["pngeren"].ToString() != "周永健")Response.Redirect("default.aspx?lb=审批");
        if (!IsPostBack)
        {
            Label1.Text = Session["pnchejian"].ToString();
            Label2.Text = Session["pn"].ToString();
            Label3.Text = Session["pngeren"].ToString();
            if (Label1.Text == Label2.Text) Label1.Visible = false;
            //this.GridView1.Columns[0].Visible = (Label2.Text == "设备科");
            g_bind();
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#D1DDF1'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                string carnum = string.Empty;
                if (this.GridView1.DataKeys[e.Row.RowIndex]["carnum"] == DBNull.Value)carnum = string.Empty;
                else carnum = (string)this.GridView1.DataKeys[e.Row.RowIndex]["carnum"];
                ((LinkButton)e.Row.Cells[14].Controls[1]).Visible = !string.IsNullOrEmpty(carnum);
                ((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除这条记录吗?')");
            }
        }
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1;
            e.Row.Cells[1].Text = id.ToString();
        }
    }
    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        string jinggao3 = "<SCRIPT language=javascript>alert('";
        jinggao3 += "删除操作成功！";
        jinggao3 += "');</SCRIPT>";
        ClientScript.RegisterStartupScript(this.GetType(), "aa3", jinggao3);
    }
    protected void ddlDW_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.ddlCarNum.Items.Clear();
        this.ddlCarNum.Items.Add(new ListItem("==请选择==", ""));
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.ddlDW.SelectedValue))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "aa1", "<SCRIPT language=javascript>alert('请选择单位！');</SCRIPT>");
            this.ddlDW.Focus();
            return;
        }
        if (string.IsNullOrEmpty(this.txtCardNum.Text.Trim()))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "aa1", "<SCRIPT language=javascript>alert('请输入卡号！');</SCRIPT>");
            this.txtCardNum.Focus();
            return;
        }
        if (string.IsNullOrEmpty(this.txtOwner.Text.Trim()))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "aa1", "<SCRIPT language=javascript>alert('请输入持卡人姓名！');</SCRIPT>");
            this.txtOwner.Focus();
            return;
        }
        if (string.IsNullOrEmpty(this.txtBalance.Text.Trim()))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "aa1", "<SCRIPT language=javascript>alert('请输入油卡的当前余额！');</SCRIPT>");
            this.txtBalance.Focus();
            return;
        }
        decimal balance = 0;
        if (!decimal.TryParse(this.txtBalance.Text.Trim(), out balance) || balance < 0)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "aa1", "<SCRIPT language=javascript>alert('油卡的当前余额必须为大于等于0的数字！');</SCRIPT>");
            this.txtBalance.Focus();
            return;
        }
        if (string.IsNullOrEmpty(this.txtOrder.Text.Trim()))
        {
            ClientScript.RegisterStartupScript(this.GetType(), "aa1", "<SCRIPT language=javascript>alert('请输入油卡的排序号！');</SCRIPT>");
            this.txtOrder.Focus();
            return;
        }
        decimal order = 0;
        if (!decimal.TryParse(this.txtOrder.Text.Trim(), out order) || order < 0)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "aa1", "<SCRIPT language=javascript>alert('油卡的排序号必须为大于等于0的数字！');</SCRIPT>");
            this.txtOrder.Focus();
            return;
        }
        try
        {
            SqlDataSource1.InsertParameters["jgname"].DefaultValue = this.ddlDW.SelectedValue.Substring(this.ddlDW.SelectedValue.IndexOf("|") + 1);
            SqlDataSource1.InsertParameters["bzname"].DefaultValue = this.ddlDW.SelectedValue.Substring(0, this.ddlDW.SelectedValue.IndexOf("|"));
            SqlDataSource1.InsertParameters["cardnum"].DefaultValue = this.txtCardNum.Text;
            SqlDataSource1.InsertParameters["carnum"].DefaultValue = this.ddlCarNum.SelectedValue;
            SqlDataSource1.InsertParameters["owner"].DefaultValue = this.txtOwner.Text;
            SqlDataSource1.InsertParameters["ownerdetail"].DefaultValue = this.txtOwnerDetail.Text;
            SqlDataSource1.InsertParameters["balance"].DefaultValue = this.txtBalance.Text;
            SqlDataSource1.InsertParameters["isshowinmain"].DefaultValue = this.cbShow.Checked.ToString();
            SqlDataSource1.InsertParameters["enabled"].DefaultValue = this.cbEnabled.Checked.ToString();
            SqlDataSource1.InsertParameters["ordernum"].DefaultValue = this.txtOrder.Text;
            SqlDataSource1.InsertParameters["addname"].DefaultValue = this.Label3.Text;
            SqlDataSource1.InsertParameters["addip"].DefaultValue = Page.Request.UserHostAddress.ToString().Trim();
            int success = SqlDataSource1.Insert();

            string jinggao3 = "<SCRIPT language=javascript>alert('";
            jinggao3 += success > 0 ? "油卡基础数据添加成功！" : "油卡基础数据添加失败";
            jinggao3 += "');</SCRIPT>";
            ClientScript.RegisterStartupScript(this.GetType(), "aa3", jinggao3);

            if (success > 0)
            {
                ResetForm();
                this.GridView1.Focus();
            }
            else
            {
                this.Button1.Focus();
            }
        }
        catch (SqlException err)
        {
            if (err.Number == 2627)
            {
                string js = "<SCRIPT language=javascript>alert('该卡号已存在，请检查后重新填写！');</SCRIPT>";
                ClientScript.RegisterStartupScript(this.GetType(), "err1", js);
            }
            else { throw; }
        }
    }
    protected void ResetForm()
    {
        this.ddlDW.SelectedValue = string.Empty;
        this.txtCardNum.Text = string.Empty;
        this.ddlCarNum.SelectedValue = string.Empty;
        this.txtOwner.Text = string.Empty;
        this.txtOwnerDetail.Text = string.Empty;
        this.txtBalance.Text = "0";
        this.cbShow.Checked = true;
        this.cbEnabled.Checked = true;
        this.txtOrder.Text = "0";
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        g_bind();
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        this.ddlDW0.SelectedValue = string.Empty;
        this.txtCardNum0.Text = string.Empty;
        this.txtCarNum0.Text = string.Empty;
        this.txtOwner0.Text = string.Empty;
        this.cbEnabled0.Checked = true;
        this.cbShow0.Checked = true;
        g_bind();
    }
    public void g_bind()
    {
        Label_tiaojian.Text = "当前选择条件:";
        string sqlstr = "SELECT * FROM [youkaguanli_card] where 1=1 ";
        if (!string.IsNullOrEmpty(this.ddlDW0.SelectedValue.Trim()))
        {
            sqlstr += " and (jgname='" + this.ddlDW0.SelectedItem.Text.Trim() + "' or bzname='" + this.ddlDW0.SelectedItem.Text.Trim() + "') ";
            Label_tiaojian.Text += "单位：'" + this.ddlDW0.SelectedItem.Text + "及下级'；  ";
        }
        if (!string.IsNullOrEmpty(this.txtCardNum0.Text.Trim()))
        {
            sqlstr += " and cardnum like '%" + this.txtCardNum0.Text.Trim() + "%' ";
            Label_tiaojian.Text += "卡号：包含'" + this.txtCardNum0.Text.Trim() + "'；  ";
        }
        if (!string.IsNullOrEmpty(this.txtCarNum0.Text.Trim()))
        {
            sqlstr += " and carnum like '%" + this.txtCarNum0.Text.Trim() + "%' ";
            Label_tiaojian.Text += "车号：包含'" + this.txtCarNum0.Text.Trim() + "'；  ";
        }
        if (!string.IsNullOrEmpty(this.txtOwner0.Text.Trim()))
        {
            sqlstr += " and owner like '%" + this.txtOwner0.Text.Trim() + "%' ";
            Label_tiaojian.Text += "持卡人：包含'" + this.txtOwner0.Text.Trim() + "'；  ";
        }
        if (this.cbShow0.Checked != this.cbShow1.Checked)
        {
            sqlstr += " and isshowinmain=" + (this.cbShow0.Checked ? "1" : "0");
            Label_tiaojian.Text += "不在主页面显示；  ";
        }
        if (this.cbEnabled0.Checked != this.cbEnabled1.Checked)
        {
            sqlstr += " and enabled=" + (this.cbEnabled0.Checked ? "1" : "0");
            Label_tiaojian.Text += "已禁用的油卡；  ";
        }
        if (Label_tiaojian.Text == "当前选择条件:") Label_tiaojian.Text = "当前选择条件:无";
        sqlstr += "ORDER BY [enabled] DESC, [ordernum],[jgname], [bzname]";
        SqlDataSource1.SelectCommand = sqlstr;
        GridView1.DataSourceID = SqlDataSource1.ID;
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        g_bind();
        ImageButton1.Focus();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "UpdateCJBZ")
        {
            //更新制定卡号的车间班组。
            string cardnum = e.CommandArgument.ToString();
            if (string.IsNullOrEmpty(cardnum)) return;
            string sqlstr2 = @"update c set jgname=chejian1,bzname=banzu1 from
youkaguanli_card c left join tcjcb_qiche on carnum=chehao where carnum is not null and carnum<>'' and cardnum='" + cardnum + "'";
            sqlcon = new SqlConnection(strCon);
            sqlcom = new SqlCommand(sqlstr2, sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            g_bind();
        }
    }
    protected void UpdateAll_Click(object sender, EventArgs e)
    {
        string sqlstr2 = @"update c set jgname=chejian1,bzname=banzu1 from
youkaguanli_card c left join tcjcb_qiche on carnum=chehao where carnum is not null and carnum<>''";
        sqlcon = new SqlConnection(strCon);
        sqlcom = new SqlCommand(sqlstr2, sqlcon);
        sqlcon.Open();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        g_bind();
    }
}